package com.hmeter.mongodb;

import java.net.UnknownHostException;
import java.util.Map;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class MongoDbUpdateSampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5390999833963494979L;
	
	private static final String HOST = "HOST";
	private static final String PORT = "PORT";
	private static final String DBNAME = "DBNAME";
	private static final String COLLECTIONNAME = "COLLECTIONNAME";
	private static final String DELIMITER = "DELIMITER";
	private static final String OPERATOR = "OPERATOR";
	private static final String QUERYLIST = "QUERYLIST";
	private static final String UPDATEVALUE = "UPDATEVALUE";

	@Override
	public SampleResult sample(Entry arg0) {
		SampleResult result = new SampleResult();
		
		StringBuilder requestData = new StringBuilder();
		requestData.append("Mongo DB host:port: " + this.getHost() +":"+ this.getPort() + "\n");
		requestData.append("Mongo DB name: " + this.getDbName() + "\n");
		requestData.append("Collection name: " + this.getCollectionName() + "\n");
		requestData.append("Query list: " + this.getQueryList() + "\n");
		requestData.append("Delimiter: " + this.getDelimiter() + "\n");
		requestData.append("Operator on query list: " + this.getOperator() + "\n");
		requestData.append("Update value: " + this.getUpdateValue() + "\n");
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData.toString());
		result.setDataType(SampleResult.TEXT);
		
		result.sampleStart();

		try 
		{
			MongoDbUpdateSamplerUtility mongoUpdate = new MongoDbUpdateSamplerUtility(this.getHost(), Integer.parseInt(this.getPort()), this.getDbName(), this.getCollectionName());
			Map<String, Object> parsedUpdateContent = mongoUpdate.getMongoDbUtility().parseInputContent(this.getUpdateValue(), this.getDelimiter());
			result.setResponseData(mongoUpdate.updateExistingDocumentsInCollection(mongoUpdate.getMongoDbUtility().parseQueryInputContent(this.getQueryList(), this.getDelimiter()), this.getOperator(), mongoUpdate.getMongoDbUtility().makeSimpleDocument(parsedUpdateContent)).getBytes());
			result.setSuccessful(true);
		}
		catch (UnknownHostException e1)
		{
			result.setResponseData(e1.getMessage().getBytes());
			result.setSuccessful(false);
		}
		catch (Exception e2)
		{
			result.setResponseData(e2.getMessage().getBytes());
			result.setSuccessful(false);			
		}

		result.sampleEnd();
		return result;
	}
	
	public void setHost(String host)
	{
		setProperty(HOST, host);
	}
	
	public String getHost()
	{
		return getPropertyAsString(HOST);
	}
	
	public void setPort(String port)
	{
		setProperty(PORT, port);
	}
	
	public String getPort()
	{
		return getPropertyAsString(PORT);
	}
	
	public void setDbName(String dbName)
	{
		setProperty(DBNAME, dbName);
	}
	
	public String getDbName()
	{
		return getPropertyAsString(DBNAME);
	}
	
	public void setCollectionName(String collectionName)
	{
		setProperty(COLLECTIONNAME, collectionName);
	}
	
	public String getCollectionName()
	{
		return getPropertyAsString(COLLECTIONNAME);
	}
	
	public void setUpdateValue(String updateValue)
	{
		setProperty(UPDATEVALUE, updateValue);
	}
	
	public String getUpdateValue()
	{
		return getPropertyAsString(UPDATEVALUE);
	}
	
	public void setOperator(String operator)
	{
		setProperty(OPERATOR, operator);
	}
	
	public String getOperator()
	{
		return getPropertyAsString(OPERATOR);
	}
	
	public void setDelimiter(String delimiter)
	{
		setProperty(DELIMITER, delimiter);
	}
	
	public String getDelimiter()
	{
		return getPropertyAsString(DELIMITER);
	}
	
	public void setQueryList(String queryList)
	{
		setProperty(QUERYLIST, queryList);
	}
	
	public String getQueryList()
	{
		return getPropertyAsString(QUERYLIST);
	}
}
